import { computed } from "vue"
import { useWindowSize, useLocalStorage, useDateFormat, useNow } from '@vueuse/core'
import { useVideoStore } from '@/store'
import { event } from 'vue-gtag'

export const getAssetsFiles = (url) => {
    return new URL(`/src/assets/imgs/${url}`, import.meta.url).href
}

const { width } = useWindowSize()
export const isMobile = computed(() => {
    return width.value < 768
})

export function handleFormatVideoPath(video) {
    return `https://resource.luckydramas.com${video.shortUrlMu}`
}

export function handleShowRhythmImg(chapter = 1) {
    const videoStore = useVideoStore()
    return chapter === videoStore.playChapter
}

export function handleFormdataVideoOptions(video) {
    return {
        id: video.shortId,
        name: video.shortTitle,
        poster: video.iconUrl,
        desc: video.shortInfo,
        browseCount: video.playbackCount,
        collectCount: video.collectCount
    }
}

export function isPwa() {
    const isFirst = useLocalStorage('isFirst')
    if (window.matchMedia('(display-mode: standalone)').matches && isFirst.value) {
        event('is_pwa')
        useLocalStorage('isFirst', true)
    }
}

export function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1)); // 生成 0 到 i 之间的随机数
        [array[i], array[j]] = [array[j], array[i]]; // 交换 array[i] 和 array[j]
    }
    return array;
}

export function getCurrentClientTime() {
    const formatted = useDateFormat(useNow(), 'YYYY-MM-DD HH:mm:ss')
    return formatted.value
}